package com.asialjim.wechat.spring.config.db;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * =====================================
 * <br/>
 * 根据主键更新数据库表
 * <br/>
 * =====================================
 *
 * @Author Asial Jim
 * @email zengyz0128@zsmarter.com
 * @Date 2020/12/16 11:17
 * @Copyright (c) ztzh inc Ztzh Co., LTD
 */
public class ApplicationUpdateById extends ApplicationUpdate{

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
        final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);

        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
                super.sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, ENTITY, ENTITY_DOT),
                tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
    }

}
